package com.flymobi.mshop.activity;

import java.util.Map;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.flymobi.mshop.R;
import com.flymobi.mshop.adapter.ImageAdapter;
import com.flymobi.mshop.util.service.ProductService;

public class ShoppingDetail extends CommonActivity {
	private ProgressDialog mydialog;
	private String id;
	private Map<String, Object> prod;
	Gallery gallery;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// requestWindowFeature(Window.FEATURE_NO_TITLE);
		// getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		setContentView(R.layout.shoppingdetail);
		// MyApplication.getInstance().addActivity(this);
		// gallery = (Gallery) this.findViewById(R.id.Gallery01);
		id = (String) (this.getIntent().getExtras().getString("id"));
		getProd(id);
	}

	Handler hdl = new Handler() {
		public void handleMessage(Message msg) {
			if (msg.what == 1) {
				mydialog.dismiss();
				String[] imgPathList = (String[]) prod.get("imgList");
				ImageAdapter adapter = new ImageAdapter(ShoppingDetail.this,
						imgPathList);
				gallery.setAdapter(adapter);

				gallery.setOnItemClickListener(new OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {
						Toast.makeText(getApplicationContext(),
								String.valueOf(position), Toast.LENGTH_SHORT)
								.show();
					}
				});
				gallery.setSelection(500);
			}
			return;
		};
	};

	public void getProd(final String id) {
		mydialog = ProgressDialog.show(ShoppingDetail.this, null, "数据加载中...",
				true);
		new Thread() {
			public void run() {
				try {
					prod = ProductService.get(id);
					Message msg = new Message();
					msg.what = 1;
					hdl.sendMessage(msg);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.start();
	}
}
